<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>


<div id="page-content">
    <div class="row">

        <div class="col-lg-12">
            <div class="panel">
                <div class="row">
                    <div class="eq-height">
                        <div class="col-sm-6 col-lg-1 eq-box-lg">
                            <div class="panel">
                                <div class="panel-body" style="height: 100%;padding-bottom: 15px">
                                    <btn id="btn_add_task" class="btn btn-primary btn-sm" style="height: 100%;line-height:100px;">
                                        <i class="ti-plus"> </i> 发起任务
                                    </btn>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6 col-lg-2 eq-box-lg">
                            <div class="panel">
                                <div class="panel-body" style="height: 100%;padding-bottom: 15px">
                                    <div class="text-center">
                                        <!--Easy Pie Chart-->
                                        <div id="demo-pie-1" class="pie-title-center mar-rgt" data-percent="0">
                                            <span class="pie-value text-thin"></span>
                                        </div>
                                        <!-- End Easy Pie Chart -->
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6 col-lg-3 eq-box-lg">
                            <div class="panel">
                                <div class="panel-heading">
                                    <h3 class="panel-title">选择项目</h3>
                                </div>
                                <div class="panel-body" style="height: 100%;padding-bottom: 15px">
                                    <div class="text-center">
                                        <select id="dictItemId" name="dictItemId" class="form-control">
                                            <option></option>
                                            <c:forEach var="item" items="${dictItems}" varStatus="status">
                                                <option value="${item.id}" <c:if test="${item.id == task.dictItem.id}"> selected="selected"</c:if>>${item.name}</option>
                                            </c:forEach>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6 col-lg-2 eq-box-lg">
                            <div class="panel">
                                <div class="panel-body" style="height: 100%;padding-bottom: 15px">
                                    <div class="list-group bg-trans" style="margin-bottom: 0px">
                                        <a id="lnkTask02" href="javascript:void(0);" class="list-group-item text-semibold text-main">
                                            <span id="spanTask02" class="badge badge-success pull-right"></span>
                                            <span class="text-main"><i class="demo-pli-inbox-full icon-lg icon-fw"></i> 执行中</span>
                                        </a>
                                        <a id="lnkTask03" href="javascript:void(0);" class="list-group-item text-semibold text-main">
                                            <span id="spanTask03" class="badge badge-success pull-right"></span>
                                            <span class="text-main"><i class="demo-pli-inbox-full icon-lg icon-fw"></i> 完成待验收</span>
                                        </a>
                                        <a id="lnkTask04" href="javascript:void(0);" class="list-group-item text-semibold">
                                            <span id="spanTask04" class="badge badge-success pull-right"></span>
                                            <span class="text-main"><i class="demo-pli-fire-flame-2 icon-lg icon-fw"></i> 已验收</span>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6 col-lg-2 eq-box-lg">
                            <div class="panel">
                                <div class="panel-body" style="height: 100%;padding-bottom: 15px">
                                    <div class="list-group bg-trans" style="margin-bottom: 0px">
                                        <a id="lnkTask05" href="javascript:void(0);" class="list-group-item text-semibold text-main">
                                            <span id="spanTask05" class="badge badge-success pull-right"></span>
                                            <span class="text-main"><i class="demo-pli-inbox-full icon-lg icon-fw"></i> 指派为责任人的任务</span>
                                        </a>
                                        <a id="lnkTask06" href="javascript:void(0);" class="list-group-item text-semibold text-main">
                                            <span id="spanTask06" class="badge badge-success pull-right"></span>
                                            <span class="text-main"><i class="demo-pli-inbox-full icon-lg icon-fw"></i> 我发布的任务</span>
                                        </a>
                                        <a id="lnkTask07" href="javascript:void(0);" class="list-group-item text-semibold">
                                            <span id="spanTask07" class="badge badge-success pull-right"></span>
                                            <span class="text-main"><i class="demo-pli-fire-flame-2 icon-lg icon-fw"></i> 我参与的任务</span>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6 col-lg-2 eq-box-lg">
                            <div class="panel">
                                <div class="panel-body" style="height: 100%;padding-bottom: 15px">
                                    <p>
                                        <btn id="btn_calender_task" class="btn btn-primary btn-hover-warning" style="width: 100%">
                                            <i class="fa fa-calendar"> </i> 任务日历
                                        </btn>
                                    </p>
                                    <p>
                                        <btn id="btn_member_task" class="btn btn-primary btn-hover-warning" style="width: 100%">
                                            <i class="fa fa-user"> </i> 任务组员
                                        </btn>
                                    </p>
                                    <p>
                                        <btn id="btn_analysis_task" class="btn btn-primary btn-hover-warning" style="width: 100%">
                                            <i class="fa fa-bar-chart"> </i> 任务分析
                                        </btn>
                                    </p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="panel-body">
                    <div class="form-inline">
                        <div class="row">
                            <div class="col-sm-12 table-toolbar-left">

                            </div>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table id="task_running_table"></table>
                        <div id="task_running_page"></div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<div id="page-calendar" style="margin: 15px 15px 0px 15px">
</div>
<script>

    // 字典列表下拉框
    $("#dictItemId").select2({
        placeholder: "请选择一个所属项目",
        allowClear: true
    });

    var totalRecord = 0;
    var catalog = 1;
    var prjId = "";
    var userId = "";
    var resetSpan = false;
    var calendarShow = false;

    $(function(){

        init_grid_task();

        $('#demo-pie-1').easyPieChart({
            barColor :'#68b828',
            scaleColor: false,
            trackColor : '#eee',
            lineCap : 'round',
            lineWidth :8,
            onStep: function(from, to, percent) {
                $(this.el).find('.pie-value').text(Math.round(percent) + '%');
            }
        });

        $("#dictItemId").change(function(){
            resetSpan = true;
            catalog = 1;
            prjId = $("#dictItemId").val();
            reloadGrid(catalog,prjId);
        })
    });

    function reloadGrid(catalog,prjId) {
        $("#task_running_table").jqGrid("setGridParam", { postData: {catalog: catalog,prjId: prjId} }).trigger("reloadGrid");
    }
    //lnkTask02
    $("#lnkTask02").click(
        function(e) {
            catalog = 2;
            reloadGrid(catalog,prjId);
        }
    );

    //lnkTask03
    $("#lnkTask03").click(
        function(e) {
            catalog = 3;
            reloadGrid(catalog,prjId);
        }
    );

    //lnkTask04
    $("#lnkTask04").click(
        function(e) {
            catalog = 4;
            reloadGrid(catalog,prjId);
        }
    );

    //lnkTask05
    $("#lnkTask05").click(
        function(e) {
            catalog = 5;
            reloadGrid(catalog,prjId);
        }
    );

    //lnkTask06
    $("#lnkTask06").click(
        function(e) {
            catalog = 6;
            reloadGrid(catalog,prjId);
        }
    );

    //lnkTask07
    $("#lnkTask07").click(
        function(e) {
            catalog = 7;
            reloadGrid(catalog,prjId);
        }
    );

    //btn_calender_task
    $("#btn_calender_task").click(
        function(e) {
            if (!calendarShow) {
                $("#page-calendar").show();
                var url = '<%=path%>/activiti/task/calendar?prjId='+prjId;
                loadURL(url,$("#page-calendar"));
                calendarShow = true;
            } else {
                $("#page-calendar").hide();
                calendarShow = false;
            }
        }
    );

    function reloadGridWithUser(catalog,prjId,userId) {
        $("#task_running_table").jqGrid("setGridParam", { postData: {catalog: catalog,prjId: prjId,userId: userId} }).trigger("reloadGrid");
    }
    //btn_member_task
    $("#btn_member_task").click(
        function(e) {
            var url = '<%=path%>/activiti/task/member?prjId='+prjId;
            __open_dialog_select('选择任务组员', url, function (dialogRef) {
                var callback = function (data) {
                    if (data) {
                        dialogRef.close();

                        // 获取 user
                        resetSpan = true;
                        catalog = 1;
                        prjId = $("#dictItemId").val();
                        userId = data;
                        reloadGridWithUser(catalog,prjId,userId);
                    } else {
                        dialogRef.enableButtons();
                    }
                };
                fn_dialog_member_select(callback);
            });
        }
    );

    function init_grid_task() {

        __init_jqgrid('task_running_table', 'task_running_page',
                '<%=path%>/activiti/task/list',
                ['所属项目', '任务名称', '任务负责人', '计划开始时间', '计划完成时间','优先级','当前状态', '操作', 'id'],
                [
                    {name: 'dictItemName', index: 'dictItemName', width: 60, sortable: true, search: false},
                    {name: 'name', index: 'name', width: 120, sortable: false, stype: 'text', searchoptions: {sopt: ['cn']}},
                    {name: 'leaders', index: 'leaders', width: 60, sortable: true, search: false},
                    {name: 'datPlanStart', index: 'datPlanStart', width: 60, sortable: false, search: false},
                    {name: 'datPlanEnd', index: 'datPlanEnd', width: 60, sortable: false, search: false},
                    {name: 'priority', index: 'priority', width: 40, sortable: false, search: false},
                    {name: 'txtStatus', index: 'txtStatus', width: 40, sortable: false, search: false},
                    {name: 'act', index: 'act', width: 350, fixed: true, sortable: false, search: false, formatter: actFormat},
                    {name: 'id', index: 'id', width: 100, key: true, hidden: true, hidedlg: true}
                ],
                true,
                {
                    postData: {catalog: catalog,prjId: prjId,userId: userId},
                    loadComplete: function() {
                        if (resetSpan) {
                            $("span[id^=spanTask]").each(function(){
                                $(this).text("");
                            });
                            resetSpan = false;
                        }
                        var records = $("#task_running_table").jqGrid('getGridParam','records');
                        if (catalog == 1) {
                            totalRecord = records;
                        } else {
                            $("#spanTask0"+catalog).text("" + records + " / " + totalRecord);
                            if (totalRecord != 0) {
                                var percent = Math.round( records / totalRecord * 100);
                                $('#demo-pie-1').data('easyPieChart').update(percent);
                            }
                        }
                    }
                }
        );

    }

    function actFormat(cellvalue, options, rowObject){
        var id = rowObject.id;
        cellvalue = '<button class="btn btn-white btn-sm" onclick="fn_task_read(\'' + id + '\');"><i class="fa fa-eye"> </i> 查看</button>';
        cellvalue += '<button class="btn btn-white btn-sm" onclick="fn_task_feedback(\'' + id + '\');"><i class="fa fa-wpforms"> </i> 任务反馈</button>';
        cellvalue += '<button class="btn btn-white btn-sm" onclick="fn_task_revise(\'' + id + '\');"><i class="fa fa-edit"> </i> 修改任务</button>';
        if(rowObject.txtStatus=="完成待验收" || rowObject.txtStatus=="验收完成"){
            cellvalue += '<button class="btn btn-white btn-sm" onclick="fn_task_statuschange(\'' + id + '\');"><i class="fa fa-share-square-o"> </i> 状态变更</button>';
        }

        return cellvalue;
    }

    function fn_task_read(id) {
        loadURL('<%=path%>/activiti/task/read/'+id, $('#page-content'));
    }

    $("#btn_add_task").click(function(){
        loadURL('<%=path%>/activiti/task/create', $('#page-content'));
    });

    function fn_task_feedback(id) {
        var url = '<%=path%>/activiti/taskfeedback/create/'+id;
        __open_dialog_form('任务反馈', url, function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();
//                    __toastr_info("任务反馈操作成功！");
                } else {
                    dialogRef.enableButtons();
                }
            };
            if (fn_task_feedback_save(callback) === false) {
//                __toastr_error("任务反馈操作失败！");
                dialogRef.enableButtons();
            }
        });
    }

    function fn_task_revise(id) {
        __confirm_dialog("确认？", "确定要修改该任务吗？", function () {
            var url = '<%=path%>/activiti/task/revise/' + id;
            loadURL(url, $('#page-content'));
        });
    }

    function fn_task_statuschange(id) {
        var url = '<%=path%>/activiti/task/scdialogs?taskId='+id;
        __open_dialog_form('状态变更', url, function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();
                    // 刷新grid
                    $('#task_running_table').trigger('reloadGrid');
                } else {
                    dialogRef.enableButtons();
                }
            };
            if (fn_task_statuschange_save(callback) === false) {
                dialogRef.enableButtons();
            }
        });
    }
</script>